/**   
 * @Title: MvcConfig.java 
 * @Package com.dragon.learn.configs 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author dragon   
 * @date 2018年8月21日 下午10:15:07 
 * @version V1.0   
 */
package com.dragon.learn.configs;

import com.dragon.learn.httpmessageconverter.MyHttpMessageConverter;
import com.dragon.learn.interceptors.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import java.util.List;

/**
 * @ClassName: MvcConfig
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author dragon
 * @date 2018年8月21日 下午10:15:07
 */

@ComponentScan("com.dragon.learn")
@Configuration
@EnableWebMvc
@EnableScheduling //开启计划任务支持 注解
public class MvcConfig extends WebMvcConfigurerAdapter{

	@Bean
	public InternalResourceViewResolver viewResolver() {
		
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		
//		viewResolver.setPrefix("/WEB-INF/classes/views/");
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		
		viewResolver.setViewClass(JstlView.class);
		
		return viewResolver;
	}
	
	@Bean
	public MyInterceptor myInterceptor() {
		return new MyInterceptor();
	}
	
	// 文件上传 
	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		
		multipartResolver.setMaxUploadSize(1024*1000);
		
		return multipartResolver;
	}
	
	@Bean
	public MyHttpMessageConverter myHttpMessageConverter() {
		
		return new MyHttpMessageConverter();
	}
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 静态资源映射
		registry.addResourceHandler("/assets/**").addResourceLocations("/WEB-INF/assets/");
	}
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(myInterceptor());
	}
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/index").setViewName("/index");
		registry.addViewController("/toupload").setViewName("/uploads");
		registry.addViewController("/sse").setViewName("/sse");
		registry.addViewController("/defer").setViewName("/defer");
	}
	
	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		
		converters.add(myHttpMessageConverter());
	}
}
